extends Area2D

@export var speed_史莱姆速度 : float = -50
@export var animator_史莱姆动画 : AnimatedSprite2D

@export var type_史莱姆类型 : int = 0

var is_die_是否死亡 : bool = false
var kill_num_被击杀次数 : int = 0
var pct_精英刷新率 : int = 3

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	# 随机创建史莱姆类型
	if randi_range(1, 10) <= pct_精英刷新率:
		type_史莱姆类型 = 1
		scale = Vector2(1.5, 1.5)
		modulate = Color.from_rgba8(255, 0, 255)
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass


func _physics_process(_delta: float) -> void:
	# 如何动画播放结束说明死亡, 就销毁自己
	if !animator_史莱姆动画.is_playing():
		queue_free()
		return

	# 如果死亡 就立即退出函数
	if is_die_是否死亡:
		return

	# 史莱姆每帧移动的距离
	position +=  Vector2(speed_史莱姆速度, 0) * _delta

	# 判断史莱姆是否移动出场景, 如果移动出场景就将其销毁
	if position.x < -260:
		queue_free()

	pass


func _on_body_entered(body: Node2D) -> void:
	if body is CharacterBody2D and body.name == 'CharacterBody_主角':
		body.game_over()

	pass # Replace with function body.


func _on_area_entered(area: Area2D) -> void:
	if area.is_in_group('子弹'):
		# 判断史莱姆是否是精英怪 如果是在判断击杀次数
		if type_史莱姆类型 == 1 and kill_num_被击杀次数 == 0:
			kill_num_被击杀次数 += 1
			scale = Vector2(1.0, 1.0)
			# 销毁子弹
			area.queue_free()
			return

		is_die_是否死亡 = true
		# 取消史莱姆的碰撞检测
		set_deferred('monitoring', false)
		animator_史莱姆动画.play('死亡')
		# 销毁子弹
		area.queue_free()

		get_tree().current_scene.label_击杀数 += 1 + kill_num_被击杀次数

		$AudioStreamPlayer2D.play()

	pass # Replace with function body.
